package com.lykj.lib_base.net.http

import com.blankj.utilcode.util.LogUtils
import com.lykj.lib_base.api.Api
import com.lykj.lib_base.constants.BConstants
import com.lykj.lib_base.internal.utils.Map2RequestBodyUtils
import com.yqwb.gamebox.internal.inter.OnDownloadListener
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.Call
import okhttp3.Callback
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.Response
import okhttp3.ResponseBody
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream


/**
 * ================================================
 * Retrofit管理类
 * ================================================
 *
 * @author gosha
 * @date 2022/9/2-16:26
 */
class RetrofitManager private constructor() {
    companion object {
        val instance = Holder.holder
        var apiService: Api? = null
        var retrofit: Retrofit? = null
    }

    private object Holder {
        val holder = RetrofitManager()
    }

    fun init() {
        retrofit = Retrofit.Builder()
            .client(OkHttpManager.getDefaultOkHttpClient())
            .baseUrl(BConstants.RELEASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()

        apiService = retrofit?.create(Api::class.java)
    }

    //上传单文件
    fun uploadSingleFile(url: String, filepath: String?, fileType: String): Call {
        val file = File(filepath)
        val multipartBodyBuilder = MultipartBody.Builder()
        multipartBodyBuilder.setType(MultipartBody.FORM)
        multipartBodyBuilder.addFormDataPart(
            "file",
            file.name,
            file.asRequestBody(fileType.toMediaType())
        )

        //构建请求体
        val requestBody: RequestBody = multipartBodyBuilder.build()
        val requestBuilder = Request.Builder()
        requestBuilder.url(url) // 添加URL地址
        requestBuilder.post(requestBody)
        val request: Request = requestBuilder.build()
        return OkHttpManager.getDefaultOkHttpClient().newCall(request)
    }

    //下载单文件
    fun downFile(url: String, filePath: String, fileName: String, listener: OnDownloadListener) {
        val request = Request.Builder().url(url).build()

        OkHttpManager.getDefaultOkHttpClient().newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                LogUtils.e("错误:${e.stackTrace}")
                listener.onFail()
            }

            override fun onResponse(call: Call, response: Response) {
                if (response.code == 200) {
                    var `is`: InputStream? = null
                    val buf = ByteArray(4096)
                    var len = 0
                    var fos: FileOutputStream? = null
                    var saveFile: File? = null

                    try {
                        `is` = response.body?.byteStream()
                        val total = response.body?.contentLength() ?: 1
                        saveFile = File("$filePath/$fileName")
                        if (saveFile == null) {
                            listener.onFail()
                            return
                        }
                        fos = FileOutputStream(saveFile)
                        var sum = 0
                        while ((`is`?.read(buf).also { len = it ?: 0 }) != -1) {
                            fos.write(buf, 0, len)
                            sum += len
                            val progress = (sum * 1f / total * 100).toInt()
                            listener.onProgress(sum.toFloat(), total.toFloat(), progress)
                        }
                        fos.flush()
                        // 下载完成
                        listener.onSuccess()
                    } catch (e: Exception) {
                        e.printStackTrace()
                        listener.onFail()
                    } finally {
                        try {
                            `is`?.close()
                        } catch (e: IOException) {
                            e.printStackTrace()
                        }
                        try {
                            fos?.close()
                        } catch (e: IOException) {
                            e.printStackTrace()
                        }
                    }
                }
            }
        })
    }

    fun downFileCall(
        url: String,
        filePath: String,
        fileName: String,
        listener: OnDownloadListener
    ): Call {
        val request = Request.Builder().url(url).build()
        val call = OkHttpManager.getDefaultOkHttpClient().newCall(request)
        call.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
//                if (!call.isCanceled()) {
                listener.onFail()
//                }
//                LogUtils.e("错误:${e.printStackTrace()}")
            }

            override fun onResponse(call: Call, response: Response) {
                if (response.code == 200) {
                    var `is`: InputStream? = null
                    val buf = ByteArray(4096)
                    var len = 0
                    var fos: FileOutputStream? = null
                    var saveFile: File? = null

                    try {
                        `is` = response.body?.byteStream()
                        val total = response.body?.contentLength() ?: 1
                        saveFile = File("$filePath/$fileName")
                        if (saveFile == null) {
                            listener.onFail()
                            return
                        }
                        fos = FileOutputStream(saveFile)
                        var sum = 0
                        while ((`is`?.read(buf).also { len = it ?: 0 }) != -1) {
                            fos.write(buf, 0, len)
                            sum += len
                            val progress = (sum * 1f / total * 100).toInt()
                            listener.onProgress(sum.toFloat(), total.toFloat(), progress)
                        }
                        fos.flush()
                        // 下载完成
                        listener.onSuccess()
                    } catch (e: Exception) {
                        e.printStackTrace()
                        listener.onFail()
                    } finally {
                        try {
                            `is`?.close()
                        } catch (e: IOException) {
                            e.printStackTrace()
                        }
                        try {
                            fos?.close()
                        } catch (e: IOException) {
                            e.printStackTrace()
                        }
                    }
                }
            }
        })
        return call
    }

    /**
     * 获取游戏首页banner
     */
    fun obtainHomeBanner(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainHomeBanner()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 发送验证码
     */
    fun senVCode(observer: CustomResponseBodyObserver<ResponseBody>, phone: String) {
        apiService?.let {
            it.sendVCode(phone)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 登录/注册
     */
    fun loginOrRegister(
        observer: CustomResponseBodyObserver<ResponseBody>,
        map: HashMap<String, Any>
    ) {
        apiService?.let {
            it.loginOrRegister(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 获取用户信息
     */
    fun obtainUserInfo(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainUserInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 获取礼包详情
     */
    fun obtainGiftDetail(
        observer: CustomResponseBodyObserver<ResponseBody>,
        map: HashMap<String, Any>
    ) {
        apiService?.let {
            it.obtainGiftDetail(Map2RequestBodyUtils.convert(map))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 申请礼包
     */
    fun applyGiftBag(
        observer: CustomResponseBodyObserver<ResponseBody>,
        map: HashMap<String, Any>
    ) {
        apiService?.let {
            it.applyGiftBag(Map2RequestBodyUtils.convert(map))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 获取游戏详情
     */
    fun obtainGameDetail(
        observer: CustomResponseBodyObserver<ResponseBody>,
        map: HashMap<String, Any>
    ) {
        apiService?.let {
            it.obtainGameDetail(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 游戏收藏/取消收藏
     */
    fun collectGame(
        observer: CustomResponseBodyObserver<ResponseBody>,
        map: HashMap<String, Any>
    ) {
        apiService?.let {
            it.collectGame(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 获取我的交易
     */
    fun obtainRoleBuy(
        observer: CustomResponseBodyObserver<ResponseBody>,
        map: HashMap<String, Any>
    ) {
        apiService?.let {
            it.obtainRoleBuy(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 购买
     */
    fun buy(
        observer: CustomResponseBodyObserver<ResponseBody>,
        tranId: String
    ) {
        apiService?.let {
            it.buy(tranId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 获取售卖详情
     */
    fun obtainSaleDetail(observer: CustomResponseBodyObserver<ResponseBody>, id: String) {
        apiService?.let {
            it.obtainSaleDetail(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 获取我的礼包
     */
    fun obtainMyGiftBag(
        observer: CustomResponseBodyObserver<ResponseBody>,
        map: HashMap<String, Any>
    ) {
        apiService?.let {
            it.myGifts(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 获取下拉游戏列表
     */
    fun obtainChooseGame(
        observer: CustomResponseBodyObserver<ResponseBody>,
        name: String
    ) {
        apiService?.let {
            it.obtainChooseGame(name)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 获取下拉区服
     */
    fun obtainChooseServer(
        observer: CustomResponseBodyObserver<ResponseBody>,
        id: String
    ) {
        apiService?.let {
            it.obtainChooseServer(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 获取下拉角色
     */
    fun obtainChooseRole(
        observer: CustomResponseBodyObserver<ResponseBody>,
        gameId: String, serverId: String
    ) {
        apiService?.let {
            it.obtainChooseRole(gameId, serverId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 添加交易
     */
    fun applyTrading(
        observer: CustomResponseBodyObserver<ResponseBody>,
        map: HashMap<String, Any>
    ) {
        apiService?.let {
            it.applyTrading(Map2RequestBodyUtils.convert(map))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 获取我的优惠券列表
     */
    fun obtainMyCoupon(
        observer: CustomResponseBodyObserver<ResponseBody>,
        map: HashMap<String, Any>
    ) {
        apiService?.let {
            it.obtainMyCoupon(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 用户登录后初始化
     */
    fun userInit(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.userInit()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 下架
     */
    fun offTheShelf(
        tranId: String,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.offTheShelf(tranId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //退出登录
    fun logout(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.logout()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取有奖任务列表
    fun obtainAwardTaskList(
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.obtainAwardTaskList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //领取任务奖励
    fun receiverAwardTask(
        taskId: String,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.receiverAwardTask(taskId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //玩过的游戏
    fun obtainPlayedGames(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.obtainPlayedGames(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取钱包记录
    fun obtainMyBilling(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.obtainMyBilling(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //支付
    fun pay2(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.pay2(Map2RequestBodyUtils.convert(map))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //检查更新
    fun checkUpdate(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.checkUpdate()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取积分明细
    fun obtainIntegralRecord(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.obtainIntegralRecord(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //福利兑换码
    fun welfareCodeExchange(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.welfareCodeExchange(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //平台币充值
    fun ptbRecharge(money: String, observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.ptbRecharge(money)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //积分兑换
    fun integralExchange(money: String, observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.integralExchange(money)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //平台币充值记录
    fun obtainPtbRechargeRecord(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.obtainPtbRechargeRecord(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取首页
    fun obtainHome(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainHome()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取回收小号列表
    fun obtainRecyclingList(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainRecyclingList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //回收角色
    fun recyclingRole(partId: String, observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.recyclingRole(partId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //回收记录
    fun recyclingRecords(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.recyclingRecords(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //购买记录
    fun buyOrSaleRecords(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.buyOrSaleRecords(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取首页会员游戏
    fun obtainHomeVipGames(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainHomeVipGames()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取可售卖角色
    fun obtainSaleRole(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainSaleRole()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //修改头像/昵称
    fun editUserInfo(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.editUserInfo(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //实名认证
    fun realNameAuth(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.realNameAuth(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //检查服务商代码
    fun checkCode(
        shareCode: String, observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.checkCode(shareCode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //代理登录
    fun agentLogin(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.agentLogin(Map2RequestBodyUtils.convert(map))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取代理用户信息
    fun obtainAgentInfo(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainAgentInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取分享链接
    fun obtainShareLink(gameId: String, observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainShareLink(gameId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //发起提现申请
    fun withdrawApply(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.withdrawApply(Map2RequestBodyUtils.convert(map))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //发起提现申请
    fun obtainWithdrawalRecord(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.obtainWithdrawalRecord(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //修改代理用户信息
    fun editAgentUserInfo(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.editAgentUserInfo(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取可购买代金券
    fun obtainCanBuyVoucher(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.obtainCanBuyVoucher(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取我的代金券
    fun obtainMyVoucher(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.obtainMyVoucher(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }


    //余额购买
    fun balanceBuy(
        id: String,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.balanceBuy(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //支付宝购买
    fun zfbBuy(
        id: String,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.zfbBuy(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //代金券赠送
    fun voucherGift(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.voucherGift(Map2RequestBodyUtils.convert(map))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取实时代理数据
    fun obtainRTData(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainRTData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取游戏攻略列表
    fun obtainGameActivity(id: String, observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainGameActivity(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取攻略与公告详情
    fun obtainActivityDetail(id: String, observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainActivityDetail(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    /**
     * 获取apk下载地址
     */
    fun obtainDownloadUrl(
        url: String,
        observer: CustomResponseBodyObserver<ResponseBody>,
    ) {
        apiService?.let {
            it.obtainDownloadUrl(url)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取收益
    fun obtainEarningRecord(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.obtainEarningRecord(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //搜索游戏
    fun searchGame(
        gameName: String,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.searchGame(gameName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取搜索推荐
    fun obtainSearchRecommend(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainSearchRecommend()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取分享收益相关信息
    fun obtainShareInfo(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainShareInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //修改代理头像昵称
    fun editAgentAvatarNickName(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.editAgentAvatarNickName(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //绑定微信
    fun bindWx(
        taskId: String,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.bindWx(taskId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取评论
    fun obtainGameComments(
        gameId: String,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.obtainGameComments(gameId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取收藏
    fun obtainMyCollect(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainMyCollect()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取热门更多游戏
    fun obtainHotGameLookMore(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.obtainHotGameLookMore(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取游戏优惠券列表
    fun obtainGameCoupons(gameId: String, observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainGameCoupons(gameId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //领取游戏优惠券
    fun receiverGameCoupon(couponId: String, observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.receiverGameCoupon(couponId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取vip礼包详情
    fun obtainVipGiftBagDetail(gameId: String, observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainVipGiftBagDetail(gameId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //申请会员礼包
    fun applyVipGiftBag(
        map: HashMap<String, Any>,
        observer: CustomResponseBodyObserver<ResponseBody>
    ) {
        apiService?.let {
            it.applyVipGiftBag(Map2RequestBodyUtils.convert(map))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }

    //获取服务端配置
    fun obtainServerConfig(observer: CustomResponseBodyObserver<ResponseBody>) {
        apiService?.let {
            it.obtainServerConfig()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer)
        }
    }
}